package com.infore.common.utils;

import com.infore.common.fw.DateUnit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class DateUtil {
	public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_SMALL_YMDHM = "yyyy-MM-dd HH:mm";
	public static final String DATE_SMALL_YMDH = "yyyy-MM-dd HH:00";
	public static final String DATE_SMALL_YMDHH = "yyyy-MM-dd HH";
	public static final String DATE_SMALL_YMDHMS = "yyyy-MM-dd HH:00:00";
	public static final String DATE_SMALL_STR  = "yyyy-MM-dd";
	public static final String HOUR_SMALL_STR  = "yyyy-MM-dd HH";
	public static final String DATE_SMALL_YM  = "yyyy-MM";
	public static final String DATE_SMALL_YY  = "yyyy";
	public static final String DATE_ALL_STR  = "yyyyMMddHHmmss";
	public static final String DATE_ALL_STR_YMDHM  = "yyyyMMddHHmm";
    /**
     * 获取当前日期
     * @return
     */
    public static String getCurrentDate(String patten){
        SimpleDateFormat format = new SimpleDateFormat(patten);
        return format.format(new Date());
    }

    /**
     * 当月最后一天
     * @return
     */
	public static String getLastDayM(Date d,String pattern) {
		SimpleDateFormat df = new SimpleDateFormat(pattern);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);
        Date theDate = calendar.getTime();
        return df.format(theDate);
	}

	/**
     * 当月最后一天(含小时),适用于年月
     * @return
     */
	public static Date getLastDayMonthM(Date d,String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);
        Date theDate = calendar.getTime();
        return theDate;
	}

	/**
     * 当月最后一天(含小时),适用于年月日
     * @return
     */
	public static Date getLastDayDayM(Date d,String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        Date theDate = calendar.getTime();
        return theDate;
	}

	/**
     *
     * 小时加减
     */
    public static String addHour(Date d,int n,String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.HOUR,n);
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    }

    public static String addHour(String d,int n,String pattern){
        SimpleDateFormat sd=new SimpleDateFormat(pattern);
    	Calendar cal = Calendar.getInstance();
        try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        cal.add(Calendar.HOUR,n);
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    }

    /**
     *
     * 日期加减
     */
    public static String addDate(Date d,int n,String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.DATE,n);
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    }

    public static String addDate(String d,int n,String pattern){
        SimpleDateFormat sd=new SimpleDateFormat(pattern);
    	Calendar cal = Calendar.getInstance();
        try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        cal.add(Calendar.DATE,n);
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    }

    /**
     *
     * 月份加减
     */
    public static String addMonth(Date d,int n,String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.MONTH,n);
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    }
    public static String addMonth(String d,int n,String pattern){
        SimpleDateFormat sd=new SimpleDateFormat(pattern);
    	Calendar cal = Calendar.getInstance();
        try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        cal.add(Calendar.MONTH,n);
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    }
    /**
     *
     * 年份加减
     */
    public static String addYear(Date d,int n,String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.YEAR,n);
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    }
    public static String addYear(String d,int n,String pattern){
        SimpleDateFormat sd=new SimpleDateFormat(pattern);
    	Calendar cal = Calendar.getInstance();
        try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        cal.add(Calendar.YEAR,n);
        Date datNew = cal.getTime();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(datNew);
    }

    public static String getYear_Month(String str) {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return sd.format(date);
    }

    /**
	 *
	 * 分钟加减
	 */
	public static String addMin(Date d, int n, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.MINUTE, n);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}
	/**
	 *
	 * 分钟加减
	 */
	public static Date addYear(Date d, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.YEAR, n);
		Date datNew = cal.getTime();
		return datNew;
	}

    /**
     * 日期转换成字符串
     * @param date
     * @return
     */
    public static String dateToString(Date date,String pattern){
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }


    /**
     * 日期转换成字符串 年 月 日
     * @param date
     * @return
     */
    public static String dateToStringYY(Date date){
        SimpleDateFormat format = new SimpleDateFormat(DATE_SMALL_STR);
        String d=format.format(date);
        d=d.substring(0,4)+"年"+d.substring(5,7)+"月"+d.substring(8)+"日";
        return d;
    }

    /**
     * 字符串转换日期
     * @param str
     * @return
     */
    public static Date stringToDate(String str,String pattern){
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        if(!str.equals("")&&str!=null){
            try {
                return format.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 查询当月天 列表 不包含今天
     * @return ["03-01","03-02"....]
     */
   public  static List<String>  dangyueDayStringList(){
	   List<String> list  = new ArrayList<String>();
	   Calendar ca = Calendar.getInstance();
	   ca.setTime(new Date());
	   int count = ca.get(Calendar.DAY_OF_MONTH);
	   ca.set(Calendar.DAY_OF_MONTH, ca.getActualMinimum(Calendar.DAY_OF_MONTH));

	   SimpleDateFormat si = new SimpleDateFormat("MM-dd");
	   String time ="";
	   int temp =1;
	   while( temp<count){
		   time = si.format(ca.getTime());
		   list.add(time);
		   ca.add(Calendar.DAY_OF_MONTH, 1);

		   temp++;
	   }
	   return list;
   }

   /**
    * 日期第一天  20
    * @param date
    * @return 2017-03-01
    */
   public static String dangyueDiyitian(Date date,String pattern){
	   SimpleDateFormat si = new SimpleDateFormat(pattern);
	   Calendar ca = Calendar.getInstance();
	   ca.setTime(date);
	   ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONDAY), ca.getActualMinimum(Calendar.DAY_OF_MONTH), 00, 00, 00);
	   return si.format(ca.getTime());
   }

   /**
    * 日期第一天(含小时),只适用于年月
    * @param date
    * @return 2017-03-01
    */
   public static Date dangyueDiyitianMonth(Date date,String pattern){
	   Calendar ca = Calendar.getInstance();
	   ca.setTime(date);
	   ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONDAY), ca.getActualMinimum(Calendar.DAY_OF_MONTH), 00, 00, 00);
	   return ca.getTime();
   }

   /**
    * 日期第一天(含小时),只适用于年月日
    * @param date
    * @return 2017-03-01
    */
   public static Date dangyueDiyitianDay(Date date,String pattern){
	   Calendar ca = Calendar.getInstance();
	   ca.setTime(date);
	   ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONDAY), ca.get(Calendar.DAY_OF_MONTH), 00, 00, 00);
	   return ca.getTime();
   }

   /**
    * *
    */
   public static Date addDate2(Date date,int n){
	   Calendar cal = Calendar.getInstance();
       cal.setTime(date);
       cal.add(Calendar.DATE,n);
       Date datNew = cal.getTime();
	   return datNew;
   }

	/**
	 * 获取年份
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);
	}
	/**
	 * 获取年份2
	 * @param date
	 * @return
	 */
	public static int getYear(String date) {
		Date d= stringToDate(date, DATE_ALL_STR);
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		return cal.get(Calendar.YEAR);
	}

	/**
	 * 获取天数
	 * @param date
	 * @return
	 */
	public static int getDAY_OF_YEAR(String date,String pattern) {
		Date d= stringToDate(date, pattern);
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		return cal.get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 获取月份
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH);
	}

	/**
	 * 获取月份
	 * @param date
	 * @return
	 */
	public static int getDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DATE);
	}

	/**
	 * 获取一年起始时间
	 * @param date
	 * @return
	 */
	public static Date getFirstDay_Year(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int firstDay = cal.getActualMinimum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, firstDay);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	public static String getFirstDay_Year(Date date,String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int firstDay = cal.getActualMinimum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, firstDay);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
	    return format.format(datNew);
	}
	/**
	 * 获取一年结束时间
	 * @param date
	 * @return
	 */
	public static Date getFinalyDay_Year(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int finalDat = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, finalDat);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}

	public static String getFinalyDay_Year(Date date,String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int finalDat = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, finalDat);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
	    return format.format(datNew);
	}

	public static String getFirstDay_Month(String date,String pattern,String pattern2) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(new SimpleDateFormat(pattern).parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int firstDat = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, firstDat);
		cal.set(Calendar.HOUR_OF_DAY, 00);
		cal.set(Calendar.MINUTE, 00);
		cal.set(Calendar.SECOND, 00);
	    return new SimpleDateFormat(pattern2).format(cal.getTime());
	}
	public static String getFinalyDay_Month(String date,String pattern,String pattern2) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(new SimpleDateFormat(pattern).parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int finalDat = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, finalDat);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
	    return new SimpleDateFormat(pattern2).format(cal.getTime());
	}

	/**
	 * 获取时间区间
	 *
	 * @param d1 开始时间区间
	 * @param d2 结束时间区间
	 * @return 时间区间集合
	 */
	public static List<String> getTimeList(String d1, String d2, String pattern, String type) {
		SimpleDateFormat sd = new SimpleDateFormat(pattern);
		Calendar c = Calendar.getInstance();
		List<String> list = new ArrayList<String>();
		Date st, et;
		long sl, el, length;
		try {
			st = sd.parse(d1);
			et = sd.parse(d2);
			sl = st.getTime();
			el = et.getTime();
			if (type == "hour") {// 以小时为间隔
				length = (el - sl) / (60 * 60 * 1000);
			} else if (type == "day") {// 一天为间隔
				length = (el - sl) / (24 * 60 * 60 * 1000);
			} else {// 一月为间隔
				/**当时间段过长时，会产生误差，因为存在闰年平年**/
				String d1Int = d1.substring(5).startsWith("0")?d1.substring(6, 7):d1.substring(5,7);
				String d2Int = d2.substring(5).startsWith("0")?d1.substring(6, 7):d2.substring(5,7);
				if (Integer.parseInt(d1Int) <= 2 && Integer.parseInt(d2Int) >=2 ) {
					length = (el - sl) / (29L * 24L * 60L * 60L * 1000L);//含有二月份的执行
				}else {
					length = (el - sl) / (30L * 24L * 60L * 60L * 1000L); //必须加L，否则溢出结果为负数
				}
			}
			list.add(sd.format(st));
			for (int i = 0; i < length; i++) {
				c.setTime(st);
				if (type == "hour") {
					c.add(Calendar.HOUR, 1);
					st = c.getTime();
				} else if (type == "day") {
					c.add(Calendar.DAY_OF_YEAR, 1);
					st = c.getTime();
				} else {
					c.add(Calendar.MONTH, 1);
					st = c.getTime();
				}
				list.add(sd.format(st));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	* @Title: getWeek
	* @Description: 根据日期字符串判断是当月第几周
	* @param str
	* @return
	* @throws Exception    参数
	* @return int    返回类型
	* @throws
	 */
	public static int getMonthWeek(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 第几周
		int week = calendar.get(Calendar.WEEK_OF_MONTH);
		// 第几天，从周日开始
		// int day = calendar.get(Calendar.DAY_OF_WEEK);
		return week;
	 }

	//得到开始结束年份之间的所有年份
	public static List<String> getTimeYear(String start,String end){
		int startTime = Integer.valueOf(start);
		int endTime = Integer.valueOf(end);
		List<String> time = new ArrayList<String>();
		if (startTime <= endTime) {
			while(startTime != endTime) {
				time.add(startTime+"");
				startTime ++;
			}
			time.add(endTime+"");
		}
		return time;
	}

	//获取当前系统时间
	public static Date getSystemTime(){
		Date enabledTimeDate = new Date();

		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			enabledTimeDate= sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return enabledTimeDate;
	}

	public static Date getSystemTime(String pattern){
		Date enabledTimeDate = new Date();

		SimpleDateFormat sdf=new SimpleDateFormat(pattern);
		try {
			enabledTimeDate= sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return enabledTimeDate;
	}

	/**
    * @Title: getDaysByYearMonth
    * @Description: 获取月份的天数
    * @param year
    * @param month
    * @return    参数
    * @return int    返回类型
    * @throws
     */
    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    // 计算两个时间的相差天数
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2) { // 同一年
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { // 闰年
                    timeDistance += 366;
                } else { // 不是闰年
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else { // 不同年
            return day2 - day1 + 1;
        }
    }

    // 根据年份与周数计算周的开始与结束时间
    public static Map<String, String> weekStartEndTime(int year, int week) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, 0, 1);
        int dayOfWeek = 7 - calendar.get(Calendar.DAY_OF_WEEK) + 1; // 算出第一周还剩几天 +1是因为1号是1天
        week = week - 2; // 周数减去第一周再减去要得到的周
        calendar.add(Calendar.DAY_OF_YEAR, week * 7 + dayOfWeek);
        String startTime = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        calendar.add(Calendar.DAY_OF_YEAR, 6);
        String endTime = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        Map<String, String> map = new HashMap<String, String>();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        return map;
    }

	/**
	* @Title: getWeek
	* @Description: 根据日期字符串输出中间的周数
	* @return
	* @throws Exception    参数
	* @return yyyy年第MM周    返回类型List
	* @throws
	 */
	public static List<String> getYearWeek(String start,String end) {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		List<String> list = new ArrayList<String>();
		//List<String> list = new ArrayList<String>();
		Date st, et;
		long sl, el, length;
		try {
			st = sd.parse(start);
			et = sd.parse(end);
			sl = st.getTime();
			el = et.getTime();
			length = (el - sl) / (24 * 60 * 60 * 1000);
			c.setTime(st);
			int year1 = c.get(Calendar.YEAR);
			int week1 = c.get(Calendar.WEEK_OF_YEAR);
			String msg1 = year1 + "年第" + week1 + "周";
			list.add(msg1);
			for (int i = 0; i < length; i++) {
				c.add(Calendar.DAY_OF_YEAR, 1);
				st = c.getTime();
				c.setTime(st);
				int year = c.get(Calendar.YEAR);
				int week = c.get(Calendar.WEEK_OF_YEAR);
				String msg = year + "年第" + week + "周";
				list.add(msg);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		list = list.stream().distinct().collect(Collectors.toList());
		return list;
	}

	/**
	* @Title: getWeek
	* @Description: 根据日期字符串判断是当月第几周
	* @param str
	* @return
	* @throws Exception    参数
	* @return yyyy年第x周    返回类型
	* @throws
	 */
	public static String getMonthWeekString(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 第几周
		int year = calendar.get(Calendar.YEAR);
		int week = calendar.get(Calendar.WEEK_OF_YEAR);
		String msg = year + "年第" + week + "周";
		// 第几天，从周日开始
		// int day = calendar.get(Calendar.DAY_OF_WEEK);
		return msg;
	 }

	public static String getYearString(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		return year + "";
	}

	public static String getWeekString(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 第几周
		int week = calendar.get(Calendar.WEEK_OF_YEAR);
		return week + "";
	}

	/**
	 * @Title: getWeek
	 * @Description: 根据日期字符串判断是当月第几周
	 * @param str
	 * @return
	 * @throws Exception    参数
	 * @return yyyy年第x周    返回类型
	 * @throws
	 */
	public static String getMonthWeekStringUSA(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
//		calendar.setFirstDayOfWeek(Calendar.SUNDAY);
		calendar.setMinimalDaysInFirstWeek(7);
		calendar.setTime(date);
		// 第几周
		int year = calendar.get(Calendar.YEAR);
		int week = calendar.get(Calendar.WEEK_OF_YEAR);
		String msg = year + "年第" + week + "周";
		// 第几天，从周日开始
		// int day = calendar.get(Calendar.DAY_OF_WEEK);
		return msg;
	}

	public static String getQuarter(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		String quarter = null;
		switch (calendar.get(Calendar.MONTH)) {
		case 1:
		case 2:
		case 3:
			quarter = "1";
			break;
		case 4:
		case 5:
		case 6:
			quarter = "2";
			break;
		case 7:
		case 8:
		case 9:
			quarter = "3";
			break;
		case 10:
		case 11:
		case 12:
			quarter = "4";
			break;
		}
		return quarter;
	}

	/**
	 * 获取两个日期字符串之间的所有年份/月份/天/小时（左闭右闭）
	 * 如果dataUnit未HOUR时 startDateStr，endDateStr的格式需和pattern一致
	 * @param startDateStr  yyyy-MM-dd
	 * @param endDateStr    yyyy-MM-dd
	 * @param dateUnit
	 * @param pattern yyyy-MM-dd yyyy-MM-dd:HH
	 * @return
	 */
	public static List<String> getDateStrBetween(String startDateStr, String endDateStr,  DateUnit dateUnit,String pattern) {
		List<String> result = new ArrayList<>();
		SimpleDateFormat sdf;
		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();

		try {
			sdf = new SimpleDateFormat(pattern);
			start.setTime(sdf.parse(startDateStr));
			end.setTime(sdf.parse(endDateStr));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar curr = start;

		switch (dateUnit) {
			case YEAR:
				sdf = new SimpleDateFormat(pattern);
				while (curr.before(end)) {
					result.add(sdf.format(curr.getTime()));
					curr.add(Calendar.YEAR, 1);
				}
				break;
			case MONTH:
				sdf = new SimpleDateFormat(pattern);
				while (curr.before(end)) {
					result.add(sdf.format(curr.getTime()));
					curr.add(Calendar.MONTH, 1);
				}
				break;
			case DAY:
				sdf = new SimpleDateFormat(pattern);
				while (curr.before(end)) {
					result.add(sdf.format(curr.getTime()));
					curr.add(Calendar.DAY_OF_MONTH, 1);
				}
				break;
			case HOUR:
				sdf = new SimpleDateFormat(pattern);
				while (curr.before(end)) {
					result.add(sdf.format(curr.getTime()));
					curr.add(Calendar.HOUR, 1);
				}
				break;
			default:
				sdf = new SimpleDateFormat(DateUtil.DATE_SMALL_YY);
				while (curr.before(end)) {
					result.add(sdf.format(curr.getTime()));
					curr.add(Calendar.YEAR, 1);
				}
				break;
		}

		// 最后要加上 endDateStr 所属的年份/月份/天（保证不漏）
		result.add(sdf.format(end.getTime()));
		return result.stream().distinct().collect(Collectors.toList());
	}

	public static void main(String[] args) {
//		List<String> list = getDateStrBetween("2021-01-01", "2021-02-01", DateUnit.MONTH, "yyyy-MM");
		List<String> list = getDateStrBetween("2021-01-01 00:00:00", "2021-01-02 12:00:00", DateUnit.HOUR, "yyyy-MM-dd HH");
		for (String y : list){
			System.out.println(y);
		}
	}
}
